#!/bin/sh /etc/rc.common
# Copyright (C) 2008-2020 OpenWrt.org
# Copyright (C) 2022 ImmortalWrt.org

START=99
USE_PROCD=0

start_vlan(){
    local cfg="$1"

    local type
    config_get type "$cfg" TYPE

    case "$type" in
    wifi-iface)
        local network ifname disabled

        config_get network "$cfg" 'network'
        config_get ifname "$cfg" 'ifname'
        config_get disabled "$cfg" 'disabled'

        [ "$network" = "lan" ] && return 1
        [ "$disabled" = "1" ] && return 1

        echo "set $ifname vlan"
        # query belong into bridge interface
        local brlan_path="/sys/class/net/$ifname/brport/bridge"
        local br_ifname
        br_ifname=$(readlink "$brlan_path" | awk -F'/' '{printf $3}')
        brctl delif $br_ifname $ifname 2>/dev/null
        [ $? -ne 0 ] && {
            echo "1 brctl delif $br_ifname $ifname failed" >> /tmp/testcmd
        }
        brctl delif "br-lan" $ifname 2>/dev/null
        [ $? -ne 0 ] && {
            echo "2 brctl delif br-lan $ifname failed" >> /tmp/testcmd
        }

        # compare bridge interface, if same, then exist
        [ "br-$network" = "$br_ifname" ] && return 1
        #change bridge interface
        echo "$ifname has been changed to br-$network" 2>/dev/null
        brctl delif $br_ifname $ifname 2>/dev/null
        [ $? -ne 0 ] && {
            echo "3 brctl delif $br_ifname $ifname failed" >> /tmp/testcmd
        }
        brctl addif br-$network $ifname 2>/dev/null
        [ $? -ne 0 ] && {
            echo "brctl addif br-$network $ifname failed" >> /tmp/testcmd
        }

        # add inteface to firewall lan's list
        iptables -D INPUT -i br-$network -j ACCEPT 2>/dev/null
        iptables -D OUTPUT -o br-$network -j ACCEPT 2>/dev/null
        iptables -D FORWARD -i br-$network -j ACCEPT 2>/dev/null
        iptables -D FORWARD -o br-$network -j ACCEPT 2>/dev/null

        iptables -I INPUT -i br-$network -j ACCEPT 2>/dev/null
        iptables -I OUTPUT -o br-$network -j ACCEPT 2>/dev/null
        iptables -I FORWARD -i br-$network -j ACCEPT 2>/dev/null
        iptables -I FORWARD -o br-$network -j ACCEPT 2>/dev/null
    ;;
    esac
}

stop_vlan(){
    local cfg="$1"

    local type
    config_get type "$cfg" TYPE

    case "$type" in
    wifi-iface)
        local network ifname disabled

        config_get network "$cfg" 'network'
        config_get ifname "$cfg" 'ifname'
        config_get disabled "$cfg" 'disabled'

        echo "delete $ifname vlan"

        # query belong into bridge interface
        local brlan_path="/sys/class/net/$ifname/brport/bridge"
        local br_ifname
        local lan_ifname
        br_ifname=$(readlink "$brlan_path" | awk -F'/' '{printf $3}')
        # get origin ifname
        lan_ifname=$(readlink "$brlan_path" | awk -F'/' '{printf $3}' | tr -d 'br-')

        # compare bridge interface, if same, then exist
        [ "br-lan" = "$br_ifname" ] && return 1

        #change bridge interface
        brctl delif "$br_ifname" "$ifname" 2>/dev/null
        echo "$ifname has been changed to br-lan" 2>/dev/null
        brctl addif "br-lan" "$ifname" 2>/dev/null

        # delete inteface to firewall lan's list
        iptables -D INPUT -i br-$network -j ACCEPT 2>/dev/null
        iptables -D OUTPUT -o br-$network -j ACCEPT 2>/dev/null
        iptables -D FORWARD -i br-$network -j ACCEPT 2>/dev/null
        iptables -D FORWARD -o br-$network -j ACCEPT 2>/dev/null
    ;;
    esac
}

start_service() {
    config_load 'wireless'
    config_foreach start_vlan 'wifi-iface'
}

stop_service() {
    config_load 'wireless'
    config_foreach stop_vlan 'wifi-iface'
}

reload_service() {
    stop
    start
}

service_triggers() {
    procd_add_reload_trigger "n2n"
}
